package xhwl.pulltorefresh;

import android.animation.ValueAnimator;
import android.content.Context;
import android.support.annotation.ColorInt;
import android.support.annotation.NonNull;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.animation.LinearInterpolator;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.scwang.smartrefresh.layout.api.RefreshFooter;
import com.scwang.smartrefresh.layout.api.RefreshKernel;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.constant.RefreshState;
import com.scwang.smartrefresh.layout.constant.SpinnerStyle;


/**
 * Created by admin on 2017-11-08.
 */

public class DefaultFooter extends RelativeLayout implements RefreshFooter {
    ImageView ivFooter;
    TextView textTip;
    private ValueAnimator animator;
    int height;
    public DefaultFooter(Context context) {
        this(context,null);

    }

    public DefaultFooter(Context context, AttributeSet attrs) {
        super(context, attrs);
        LayoutInflater.from(context).inflate(R.layout.footer_default,this);
        ivFooter= (ImageView) findViewById(R.id.ivFooter);
        textTip= (TextView) findViewById(R.id.textTip);

        RelativeLayout llHeader= (RelativeLayout) findViewById(R.id.rlFooter);
        llHeader.measure(0,0);
        //获取组件的高度  
        height=llHeader.getMeasuredHeight();
    }
    public int getFooterHeight(){
        return height;
    }
    @Override
    public void onPullingUp(float percent, int offset, int footerHeight, int extendHeight) {

    }

    @Override
    public void onPullReleasing(float percent, int offset, int footerHeight, int extendHeight) {

    }

    @Override
    public boolean setLoadmoreFinished(boolean finished) {
        return false;
    }

    @NonNull
    @Override
    public View getView() {
        return this;
    }

    @Override
    public SpinnerStyle getSpinnerStyle() {
        return SpinnerStyle.Translate;
    }

    @Override
    public void setPrimaryColors(@ColorInt int... colors) {

    }

    @Override
    public void onInitialized(RefreshKernel kernel, int height, int extendHeight) {

    }

    @Override
    public void onHorizontalDrag(float percentX, int offsetX, int offsetMax) {

    }

    @Override
    public void onStartAnimator(RefreshLayout layout, int height, int extendHeight) {
        startAnimation();
    }

    @Override
    public int onFinish(RefreshLayout layout, boolean success) {
        stopAnimation();
        return 500;
    }

    @Override
    public boolean isSupportHorizontalDrag() {
        return false;
    }

    @Override
    public void onStateChanged(RefreshLayout refreshLayout, RefreshState oldState, RefreshState newState) {
        switch (newState) {

            case None:
            case PullToUpLoad:
                textTip.setVisibility(View.VISIBLE);
                textTip.setText("上拉加载更多");
                ivFooter.setVisibility(View.GONE);
                break;
//            case PullUpCanceled:
            case ReleaseToLoad:
                textTip.setVisibility(View.VISIBLE);
                textTip.setText("释放立即加载");
                ivFooter.setVisibility(View.GONE);
                break;
            case Loading:
                textTip.setVisibility(View.GONE);
                ivFooter.setVisibility(View.VISIBLE);
                break;
            case LoadFinish:
                textTip.setVisibility(View.VISIBLE);
                textTip.setText("加载完成");
                ivFooter.setVisibility(View.GONE);

                break;
        }
    }
    public void startAnimation() {
        animator = ValueAnimator.ofFloat(ivFooter.getRotation()
                ,ivFooter.getRotation()+359);
        animator.setInterpolator(new LinearInterpolator());
        animator.setRepeatCount(ValueAnimator.INFINITE);
        animator.setDuration(1000);
        animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                ivFooter.setRotation((Float) animation.getAnimatedValue());
            }
        });
        animator.start();
      /* RotateAnimation animation = new RotateAnimation(0f, 359f, Animation.RELATIVE_TO_SELF,
                0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
        animation.setInterpolator(new LinearInterpolator());
        animation.setDuration(1000);
        animation.setRepeatCount(Animation.INFINITE);
        imageLoadMore.startAnimation(animation);*/
    }
    public void stopAnimation() {
        animator.end();
//        imageLoadMore.clearAnimation();
    }
}
